package com.yuedou.jylt.facelib.engine;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import androidx.annotation.NonNull;

public class ActionEngine {
    private static ActionEngine actionEngine;
    public static ActionEngine getInstance(){
        if(actionEngine == null){
            synchronized (ActionEngine.class){
                actionEngine = new ActionEngine();
            }
        }
        return actionEngine;
    }

    private static int shouldDoAction ;
    private static long lastTime;
    public void start(){
        shouldDoAction = 0;//new Random().nextInt(2);// 0 - 1
        lastTime = 15000;
        if(thread == null){
            thread = new Thread(runnable);
            thread.start();
        }
        Message message = new Message();
        message.what = 2;
        message.arg1 = shouldDoAction ;
        handler.sendMessage(message);
    }


    private Runnable runnable = () -> {
        checkHasOverTime();
        try {
            Thread.sleep(30);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    };
    private Thread thread ;

    public void destory(){
        if(thread != null){
            thread.interrupt();
            thread = null;
        }
    }
    /**
     * 检测是否有超时
     */
    private void checkHasOverTime(){
        if(!canRunTime || lastTime < 0){
            return;
        }
        lastTime -= 30;
        if(lastTime < 0){
            Message message = new Message();
            message.what = 1;
            message.arg1 = 0 ;
            handler.sendMessage(message);
        }
    }

    private boolean canRunTime = false;
    public void onResult(int action){
        Log.e("onResult->action",shouldDoAction + "/" + action);
        if(action <= 4) {
            canRunTime = true;
            Message message = new Message();
            if (action == shouldDoAction) {
                message.what = 1;
                message.arg1 = 1;
            } else {
                message.what = 2;
                message.arg1 = shouldDoAction;
            }
//            message.what = 1;
//            message.arg1 = 1;
            handler.sendMessage(message);
        }else{
            canRunTime = false;
        }
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 1:
                    int arg1 = msg.arg1;
                    if(onActionCallback!=null){
                        boolean isOk = arg1 == 1;
                        onActionCallback.onResult(isOk,isOk ? "认证成功" : "认证失败");
                    }
                    break;
                case 2:
                    int arg = msg.arg1;
                    if(onActionCallback != null){
                        String m = "";
                        if(arg == 0 ){
                            m= "请眨眨眼";
                        }else if(arg == 1){
                            m= "请张张嘴";
                        }else if(arg == 2){
                            m= "请左右摇头";
                        }else if(arg == 3){
                            m= "请轻微抬头";
                        }else if(arg == 4){
                            m= "请轻微低头";
                        }
                        onActionCallback.onMessage(m);
                    }
                    break;
            }
        }
    };

    private OnActionCallback onActionCallback;

    public OnActionCallback getOnActionCallback() {
        return onActionCallback;
    }

    public void setOnActionCallback(OnActionCallback onActionCallback) {
        this.onActionCallback = onActionCallback;
    }

    public interface OnActionCallback{
        void onMessage(String msg);
        void onResult(boolean isOk,String msg);
    }
}
